package com.wanhe.shoppingcart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanhe.base.util.ObjectUtil;
import com.wanhe.shoppingcart.bean.CartItem;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhe.shoppingcart.manager.ICartItem;

import java.util.ArrayList;

/**
 * Created by XieZaiWei on 2017/5/9.
 * 分类子项adapter
 */

public class ChildAdapter extends BaseAdapter {
	private LayoutInflater			mInflater;
	private ArrayList <CartItem>	mArrCartItem;
	private int						mItemLayout;

	public ChildAdapter(Context context, ArrayList <CartItem> arrCartItem, int itemLayout) {
		mInflater = LayoutInflater.from(context);
		mArrCartItem = arrCartItem;
		mItemLayout = itemLayout;
	}

	@Override
	public int getCount() {
		return mArrCartItem.size();
	}

	@Override
	public ICartItem getItem(int position) {
		return mArrCartItem.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(mItemLayout, null);
			viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
			viewHolder.iv_plus = (ImageView) convertView.findViewById(R.id.iv_plus);
			viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
			viewHolder.iv_reduce = (ImageView) convertView.findViewById(R.id.iv_reduce);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final CartItem cartItem = mArrCartItem.get(position);

		if (ObjectUtil.checkObject(cartItem.getSize())) {
			viewHolder.tv_name
					.setText(cartItem.getName() + "(" + cartItem.getSize().getName() + ")");

		} else if (ObjectUtil.checkObjectList(cartItem.getTags())) {
			viewHolder.tv_name
					.setText(cartItem.getName() + "(" + cartItem.getTags().get(0).getName() + ")");
		} else {
			viewHolder.tv_name.setText(cartItem.getName());
		}

		viewHolder.tv_price.setText("￥" + cartItem.getPrice());
		viewHolder.tv_count.setText(String.valueOf(cartItem.getCount()));
		viewHolder.iv_plus.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int step = CartManager.getInstance().getAddStep(cartItem.getDishGuid());
				if (step != 0) {
					CartManager.getInstance().addItem(cartItem, step);
				}
				notifyDataSetChanged();
			}
		});

		viewHolder.iv_reduce.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int step = CartManager.getInstance().getReduceStep(cartItem.getDishGuid());
				if (step != 0) {
					CartManager.getInstance().reduceItem(cartItem, step);
				}
				notifyDataSetChanged();
			}
		});

		return convertView;
	}

	private static class ViewHolder {
		public TextView		tv_name;
		public TextView		tv_price;
		public ImageView	iv_plus;
		public TextView		tv_count;
		public ImageView	iv_reduce;
	}

}
